package com.zx.web.dao.impl;

import com.zx.bean.Address;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.AddressDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.dao.impl
 * @Description:
 * @Date 2022/9/15 11:01
 */
public class AddressDaoImpl implements AddressDao {

    @Override
    public List<Address> getAddressByUid(int userId) {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from tb_address where uid = ?";
        try {
            return qr.query(sql,new BeanListHandler<>(Address.class),userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addAddress(Address address) {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "INSERT INTO `tb_address` ( `detail`, `name`, `phone`, `uid`, `level`) VALUES ( ?, ?, ?, ?, ?)";
        try {
            int update = qr.update(sql, address.getDetail(), address.getName(), address.getPhone(), address.getUid(), address.getLevel());
            return update>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteAddress(int id) {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "delete from tb_address where id = ?";
        try {
            int update = qr.update(sql, id);
            return update>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateAddress(Address address) {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "UPDATE `tb_address` SET `detail` = ?, `name` = ?, `phone` = ?,level = ? WHERE `id` = ?";
        try {
            int update = qr.update(sql, address.getDetail(), address.getName(), address.getPhone(),address.getLevel(), address.getId());
            return update>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }


    @Override
    public void setAllAdressNormal(int userId) {
        QueryRunner qr = new QueryRunner();
        String sql = "UPDATE `tb_address` SET level = 0 WHERE `uid` = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void setOneDefaultAddress(int id) {
        QueryRunner qr = new QueryRunner();
        String sql = "UPDATE `tb_address` SET level = 1 WHERE `id` = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public Address findAddress(int aid) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_address where id = ?";
        try {
            Address query = qr.query(DataSourceUtils.getConnection(), sql, new BeanHandler<>(Address.class), aid);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
